// pages/component/tweet_list/tweet-list.js
import { 
  TOPIC_TWEET_LIST,
  TWEET_LIKE_REVERSE,
 } from "../../request/api";
 
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    listData:{
      type: Object,
      observer: function(newVal, oldVal){
        if(newVal.height){
          this.setData({
            listViewData: {
              height: newVal.height,
            }
          });
        }
        if(newVal.id){
          this.setData({
            id: newVal.id,
          });
        }
        if(newVal.name){
          this.setData({
            name: newVal.name,
          });
        }
        if(newVal.type){
          this.setData({
            type: newVal.type,
          });
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataList:[],
    pageToken: '',
    type: 1,
    id: 0,
    name: '',
    listViewData: {
      loadStatus: 2,
      height: 0,
    }
  },

  lifetimes: {
    attached: function () {
      // this.loadMore();
    },
    ready: function(){
      this.loadMore();
    },
    moved: function () {},
    detached: function () {},
  },


  // 组件所在页面的生命周期函数
  pageLifetimes: {
    show: function () {},
    hide: function () {},
    resize: function () {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadMore: function(){
      this._getTweets(this.data.id, this.data.name, this.data.pageToken, this.data.type);
    },
    refresh: function(){
      this.data.pageToken = '';
      this.data.dataList = [];
      this.loadMore();
    },
    _getTweets: function(id, name, pageToken, type){  
      TOPIC_TWEET_LIST({'id': id, 'name': name, 'pageToken': pageToken, 'type': type}).then(res => {
        if(res.data.code == 1){
          var result = res.data.result;
          var tempList = result.items;
          if(this.data.pageToken){
            tempList = this.data.dataList.concat(tempList);
          }
          this.setData({
            dataList: tempList,
            pageToken: result.nextPageToken,
            listViewData: {
              loadStatus: result.pageInfo.totalResults > tempList.length? 2 : 3,
            },
          })
        } else {
          this.setLoadStatus(2);
        }
      }).catch(res => {
        this.setLoadStatus(2);
      })
    },

    setLoadStatus: function(status){
      this.setData({
        listViewData: {
          loadStatus: status,
        },
      });
    },

    _reverseTweetLike: function(e){
      if (!this._checkLogin()) {
        return;
      }
      var sourceId = e.currentTarget.dataset.id;
      var index = e.currentTarget.dataset.index;
      TWEET_LIKE_REVERSE({'sourceId': sourceId}).then(res => {
        if(res.data.code == 1){
          var tempList = this.data.dataList;
          tempList[index].liked = res.data.result.liked;
          tempList[index].likeCount = res.data.result.likeCount;
          this.setData({
            dataList: tempList
          })
        }
      }).catch(err =>{})
    },

    _checkLogin: function () {
      var app = getApp();
      if (!app.globalData.isLogin) {
        wx.showModal({
          content: '您还没有登录，请登录',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/account/login/login',
              });
            } else if (res.cancel) {
  
            }
          }
        });
        return false;
      } else {
        return true;
      }
    },

    _toDetail: function (e) {
      var index = e.currentTarget.dataset.index;
      var data = this.data.dataList[index];
        var dataStr = JSON.stringify(data);
        wx.navigateTo({
          url: '/pages/tweet/detail/detail?tweet=' + encodeURIComponent(dataStr),
        })
    },
    _toUserProfile: function(e){
      var id = e.currentTarget.dataset.id;
      if(id){
        wx.navigateTo({
          url: '/pages/user/user?userId='+id,
        })
      }
    },
    _doNothing: function(e){},
    _showTips: function(msg){
      wx.showToast({
        title: msg,
        icon: 'none'
      })
    },
    
  }
})
